<?php

function addAdminPrivs($admin){
    global $lang;
    $addHidden = true;
    if($admin == null)
        $addHidden = false;
        
    echo '<table class="tab_details fill">
          <tr>
            <th class="tittle" colspan="2">'.$lang['ADMIN_MANAGE_COMMANDS'].'</th>
          </tr><tr>';
    addCheckbox('P_ADD', 'add_admins', $admin, false, null);
    echo '</tr><tr>';
    addCheckbox('P_EDIT', 'edit_admins', $admin, false, null);  
    echo '</tr><tr>';
    addCheckbox('P_DELETE', 'del_admins', $admin, false, null);             
    echo '</tr>
        </table>';
}

function addVipPrivs($admin){
    global $lang;
    $addHidden = true;
    if($admin == null)
        $addHidden = false;
        
    echo '<table class="tab_details fill">
          <tr>
            <th class="tittle" colspan="2">'.$lang['VIPS_MANAGE_COMMANDS'].'</th>
          </tr><tr>';
    addCheckbox('P_ADD', 'add_vips', $admin, false, null);
    echo '</tr><tr>';
    addCheckbox('P_EDIT', 'edit_vips', $admin, false, null);  
    echo '</tr><tr>';
    addCheckbox('P_DELETE', 'del_vips', $admin, false, null);             
    echo '</tr>
        </table>';
}


function addVipTPrivs($admin){
    global $lang;
    $addHidden = true;
    if($admin == null)
        $addHidden = false;
        
    echo '<table class="tab_details fill">
          <tr>
            <th class="tittle" colspan="2">'.$lang['VIPS_T_MANAGE_COMMANDS'].'</th>
          </tr><tr>';
    addCheckbox('P_ADD', 'add_vips_t', $admin, false, null);
    echo '</tr><tr>';
    addCheckbox('P_EDIT', 'edit_vips_t', $admin, false, null);  
    echo '</tr><tr>';
    addCheckbox('P_DELETE', 'del_vips_t', $admin, false, null);             
    echo '</tr>
        </table>';
}
function addSrvPrivs($admin){
    global $lang;
    $addHidden = true;
    if($admin == null)
        $addHidden = false;
        
    echo '<table class="tab_details fill">
          <tr>
            <th class="tittle" colspan="2">'.$lang['SRV_MANAGE_COMMANDS'].'</th>
          </tr><tr>';
    addCheckbox('P_ADD', 'add_srv', $admin, false, null);
    echo '</tr><tr>';
    addCheckbox('P_EDIT', 'edit_srv', $admin, false, null);  
    echo '</tr><tr>';
    addCheckbox('P_DELETE', 'del_srv', $admin, false, null);             
    echo '</tr>
        </table>';
}

function updateAdmin(){
    
    global $prefix;
    $fields = array(
        'add_admins' => 0,
        'edit_admins' => 0,
        'del_admins' => 0,
        'add_vips' => 0,
        'edit_vips' => 0,
        'del_vips' => 0,
        'add_vips_t' => 0,
        'edit_vips_t' => 0,
        'del_vips_t' => 0,
        'add_srv' => 0,
        'edit_srv' => 0,
        'del_srv' => 0
    );
    $id = $_POST['id'];
  
    $query = createUpdateStatement($fields, $prefix.'_ADMINS', true, 'id', $id);
    $result = mysql_query($query);
    if(!$result){
        $error = mysql_error();
        echo $error;
		echo '<br/>';
		$commit = false;
    } 
}

function createNewAdmin(){
    global $prefix;
    global $lang;
   
    $name='';
    if(isset($_POST['name']))
        $name = $_POST['name'];
    if($name == ''){
        echo $lang['NAME_REQ'];
        echo '<br/>';
        return;
    }
    $pass='';
    $pass_repeate='';
    if(isset($_POST['pass']))
        $pass = trim($_POST['pass']);
    if(isset($_POST['pass_repeate']))
        $pass_repeate = trim($_POST['pass_repeate']);   
    if($pass==''){
        echo $lang['PASS_REQ'];
        echo '<br/>';
        return;
    }  
    if($pass!=$pass_repeate){
        echo $lang['PASS_MISMATCH'];
        echo '<br/>';
        return;
    }   
    $fields = array(
        'name' => '',
        'pass' => '',
        'add_admins' => 0,
        'edit_admins' => 0,
        'del_admins' => 0,
        'add_vips' => 0,
        'edit_vips' => 0,
        'del_vips' => 0,
        'add_vips_t' => 0,
        'edit_vips_t' => 0,
        'del_vips_t' => 0,
        'add_srv' => 0,
        'edit_srv' => 0,
        'del_srv' => 0
    );
    $query = 'SELECT 1 FROM '.$prefix.'_ADMINS WHERE name = \''.mysql_escape_string($name).'\'';
    $result = mysql_query($query);
    if($result){
        if(mysql_num_rows($result)>0){
            echo $lang['NAME_TAKEN'];
            echo '<br/>';
            return;
        }
    }
    $query = createInsertStatement($fields, $prefix.'_ADMINS', true);
    $result = mysql_query($query);
    if(!$result){
        echo mysql_error();
		echo '<br/>';
    } 
}
function deleteAdmin(){
    global $prefix;
    if($_POST['id']==1){
        echo $lang['DELETE_MAIN_ADMIN'];
        echo '<br/>';
        return;
    }
    $query = 'DELETE FROM '.$prefix.'_ADMINS WHERE id = '.$_POST['id'];
    $result = mysql_query($query);
	if(!$result){
        echo mysql_error();
        echo '<br/>';		
    }
}

function showAdminDetails($id, $isEdit){
    global $lang;
    global $prefix;
    $isNew = false;
    if($id != null){
        $query = 'SELECT * FROM '.$prefix.'_ADMINS WHERE id = '.$id;
    	$result = mysql_query($query);
    	if(!$result){
            echo mysql_error();
            echo '<br/>';
        } else {
            $admin = mysql_fetch_array($result);
        }
    }
    else {
        $admin = null;
        $isNew = true;
    }
	echo '         <form class="fill" name="form_admin" method="POST" action="main.php?action=admin">
                      <input name="id" type="hidden" value="'.$admin['id'].'"/>
                      <table class="fill">
                        <tr>
                          <td>
                            <table class="tab_details fill">	
                              <tr>
                                <td colspan="2">
                                    <table class="fill">
                                        <tr>';
                                          if($isNew){
                                                addText('NAME', 'name', null, '', null, false);
                                                echo '</tr>
                                                <tr>
                                                    <th class="right">'.$lang['PASSWORD'].'</td>
                                                    <td>
                                                        <input type="password" id="pass" name="pass"/>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <th class="right">'.$lang['PASSWORD_REPEATE'].'</td>
                                                    <td>
                                                        <input type="password" id="pass_repeate" name="pass_repeate"/>
                                                    </td>
                                                </tr>';
                                          } else {
                                            echo '  <th class="right half">'.$lang['NAME'].'</th>
                                                    <td class="left readonly_value">
                                                      '.$admin['name'].'
                                                    </td>';
                                          }
                              echo'      </tr>
                                    </table>
                                </td>
                              </tr>
                              <tr>
                                <th colspan="2" class="tittle">'.$lang['MANAGE'].'</td>
                              </tr><tr>
                                <td class="half">';
                        addAdminPrivs($admin);
                        echo '</td><td class="half">';
                        addSrvPrivs($admin);
                        echo '</td></tr><tr><td class="half">';
                        addVipPrivs($admin);
                        echo '</td><td class="half">';
                        addVipTPrivs($admin);
                        echo'
                              </td>
      				                </tr>
                            </table>
                          </td>
                        </tr>
                        <tr><td colspan="2" class="right">';
    if($isNew) {
        echo '<input class="btn" type="submit" name="ins" value="'.$lang['B_SAVE'].'"/>';
    } else if(hasAccess('edit_admins') && $admin['id']!=1) {
        echo '<input class="btn" type="button" name="edit" value="'.$lang['B_EDIT'].'" onclick="toogleInputs();"/>'; 
        echo '<input class="btn save" type="submit" disabled="true" name="upd" id="upd" value="'.$lang['B_SAVE'].'"/>';
    } 
    if(!$isNew && hasAccess('del_admins') && $admin['id']!=1){
        echo '<input class="btn" type="submit" name="del" value="'.$lang['B_DELETE'].'" onclick="return confirmDelete()"/>';
    }
    echo '           </td></tr></table></form>';
    
    if($isEdit || $isNew){
        echo '<script type="text/javascript">
                enableInputs(true);
        </script>';
    } 
}
?>
